package com.sqx.modules.disease.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.disease.dao.PrescriptMaterialsDao;
import com.sqx.modules.disease.entity.Diagnostic;
import com.sqx.modules.disease.entity.PrescriptMaterials;
import com.sqx.modules.disease.service.DiagnosticService;
import com.sqx.modules.disease.service.PrescriptMaterialsService;
import com.sqx.modules.disease.service.PrescriptService;
import com.sqx.modules.disease.utils.ChangLiangChi;
import com.sqx.modules.disease.utils.ResponseData;
import com.sqx.modules.disease.vo.DiseaseVO;
import com.sqx.modules.disease.vo.PrescriptVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 药方明细表(PrescriptMaterials)表服务实现类
 *
 * @author makejava
 * @since 2023-11-20 21:53:25
 */
@Service("prescriptMaterialsService")
@Slf4j
public class PrescriptMaterialsServiceImpl extends ServiceImpl<PrescriptMaterialsDao, PrescriptMaterials> implements PrescriptMaterialsService {

    @Resource
    DiagnosticService diagnosticService;
    @Resource
    PrescriptService prescriptService;

    @Override
    public ResponseData prescriptionGeneration(DiseaseVO diseaseVO) {
        //获取病症对应的药方
        LambdaQueryWrapper<Diagnostic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Diagnostic::getDiseaseId, diseaseVO.getDiseaseId());
        queryWrapper.eq(Diagnostic::getYinyangId, diseaseVO.getYinyangId());
        List<Diagnostic> list = diagnosticService.list(queryWrapper);
        if (list.size() == 0) {
            return new ResponseData("-1", "未找到合适病症");
        }
        log.info("获取所有病症、阴阳、脏腑信息！！");
        Map<Long, String> map = new HashedMap();
        for (Diagnostic diagnostic : list) {
            Boolean data = getData(diseaseVO.getTongueName().split(";"), diagnostic.getTongueName());
            Boolean data1 = getData(diseaseVO.getSmellName().split(";"), diagnostic.getSmellName());
            Boolean data2 = getData(diseaseVO.getSurName().split(";"), diagnostic.getSurName());
            Boolean data3 = getData(diseaseVO.getPulseName().split(";"), diagnostic.getPulseName());
            Boolean data4 = getData(diseaseVO.getDiagnoedName().split(";"), diagnostic.getDiagnoedName());
            if( data | data1 | data2 | data3 | data4){
                map.put(diagnostic.getVisceraId(), "");
            }

        /*    if (StringUtils.isNotBlank(diseaseVO.getTongueName())&&StringUtils.isNotBlank(data)) {
                newList.add(diagnostic);
            }
            if (StringUtils.isNotBlank(diseaseVO.getSmellName())&&StringUtils.isNotBlank(data)) {
                newList.add(diagnostic);
            }
            if (StringUtils.isNotBlank(diseaseVO.getSurName())&&StringUtils.isNotBlank(data)) {
                newList.add(diagnostic);
            }
            if (StringUtils.isNotBlank(diseaseVO.getPulseName())&&StringUtils.isNotBlank(data)) {
                newList.add(diagnostic);
            }
            if (StringUtils.isNotBlank(diseaseVO.getDiagnoedName())&&StringUtils.isNotBlank(data)) {
                newList.add(diagnostic);
            }*/
        }
        log.info("所有匹配脏腑配置，开始过滤相同脏腑配置");
        for (Long integer : map.keySet()) {
            String key = map.get(integer);
            for (Long str : map.keySet()) {
                String set = map.get(str);
                if (!integer.equals(str) && key.contains(set)) {
                    map.remove(integer);
                }
            }
        }
        log.info("开始合并药方！！");
        list.stream().filter(a -> map.containsKey(a.getVisceraId())).distinct().collect(Collectors.toList());
        //治则
        diseaseVO.setPlanName(list.stream().map(Diagnostic::getPlanName).collect(Collectors.joining(";")));
        List<Long> collect = list.stream().map(Diagnostic::getVisceraId).distinct().collect(Collectors.toList());
        List<String> longs=new ArrayList<>();
        for (Long aLong : collect) {
            longs.add(ChangLiangChi.VISCERA_MAP.get(aLong + "")) ;
        }
        diseaseVO.setVisceraIdList(longs);
        //药方集合
        List<Long> prescript = list.stream().filter(a->null!=a.getPrescriptId()).map(Diagnostic::getPrescriptId).distinct().collect(Collectors.toList());
        if (prescript.size() > 0) {
            //所有药材
            List<PrescriptMaterials> prescriptMaterials = this.list(new LambdaQueryWrapper<PrescriptMaterials>().in(PrescriptMaterials::getPrescriptId, prescript));
            diseaseVO.setPrescriptVOList(prescriptMaterials.stream().map(a -> {
                PrescriptVO prescriptVO = new PrescriptVO();
                prescriptVO.setMaterialsName(a.getMaterialsName());
                prescriptVO.setDosage(a.getDosage());
                return prescriptVO;
            }).collect(Collectors.toList()).stream().sorted(Comparator.comparing(PrescriptVO::getDosage)
                    .thenComparing(PrescriptVO::getMaterialsName).reversed())
                    .collect(Collectors.toList()));
            return new ResponseData().setData(diseaseVO).ok();
        } else {
            return new ResponseData("-1", "未找到合适药方！！");
        }
    }

    private boolean getData(String[] split, String tongueName) {
        if (split == null || StringUtils.isBlank(tongueName)) {
            return false;
        }
        for (String key : split) {
            if (tongueName.contains(key + ";")) {
               return true;
            }
        }
        return false;
    }
}

